<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <p id="oP"></p>
    <input type="text" id="oInput" />
    <script>
      // 观察者模式：可以观察到数据的变化，把变化之后的状态交给 watcher，watcher 调用 update 方法去做一些操作
      class Dep {
        constructor() {
          // 里面装观察者
          this.subs = []
          this.state = 88888
        }
        addSub(sub) {
          // 添加观察者
          this.subs.push(sub)
        }
        notify(state) {
          this.subs.forEach((sub) => sub.update(state))
        }
        setState(state) {
          this.state = state
          this.notify(state)
        }
      }

      class Wather {
        update(state) {
          console.log('用' + state + '治疗感冒')
        }
      }

      const dep = new Dep()
      const watcher = new Wather()
      dep.addSub(watcher)
      dep.addSub(watcher)

      dep.setState(999)
    </script>
  </body>
</html>
